package com.example.springbootvue.Controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
@RestController
public class FileUpLoadController {
    @PostMapping("/upload")
    public String uploadFile(String name, MultipartFile file, HttpServletRequest request) {
       System.out.println(name);
        // 获取上传文件的名称
        String fileName = file.getOriginalFilename();
        // 获取上传文件的路径
        String path = request.getServletContext().getRealPath("/upload");
        try {
            //不存在则创建
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdir();
            }
             file.transferTo(new java.io.File(path + "/" + fileName));
            System.out.println(file);
            System.out.println("上传成功");
            return "success";
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
